﻿#ifndef LITEDB_H
#define LITEDB_H

#include "globalinstance.h"
#include <QObject>
#include <QSqlDatabase>

class LiteDb : public GlobalInstance<LiteDb>
{
    friend class GlobalInstance<LiteDb>;

    LiteDb();
public:
    bool beginTransaction();
    bool commit();
    bool rollback();

    void init_user_db(const QString& my_id);
    bool open_user_db();
    void close_user_db();

    QMap<qint64, QVariant> query_contact();
    void update_contact(const QVariantList &vars);
    void delete_contact(const qint64 uid);
    static QStringList contact_fields();

    qint64 query_room_updatetime();
    QMap<qint64, QVariant> query_rooms();
    void update_room(const QVariantList& vars);
    void update_room(qint64 rid, const QVariantMap& vars);
    void delete_room(qint64 rid);

    QVariantList query_sessions();
    void update_session(const QChar& type, const QString& id, const QString& name, qint64 timestamp, uint style);
    void delete_session(const QChar& type, const QString& id);

    static const QByteArray create_mid(uint curtime, const QString& msg);
    static const QByteArray create_mid(uint curtime, const QByteArray& msg);
    bool insert_chat_record(const QString& suffix, bool crowd, QVariantList& params);
    bool has_chat_record(const QString& suffix);
    QStringList query_chat_record_tables();
    QVariantList query_chat_record(const QString& suffix, const QString& extra = QString(), const QString& extraEnd = QString());
    QVariantList query_chat_record_last_time(const QString& suffix);
    bool change_chat_record_step(const QString& suffix, int step, QStringList& params);
    bool change_chat_record_time(const QString& suffix, qint64 time, QString& mid);
    bool delete_chat_record(const QString& suffix, bool drop = false);
private:
    QSqlDatabase ltdb_user;
    QString  myid_;
};

#endif // LITEDB_H
